package com.lehecai.core.api.business;

import com.lehecai.core.api.AbstractApiResultBean;
import com.lehecai.core.lottery.BankType;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

/**
 * Created by zhangzq on 14-9-5.
 */
public class BankWeight extends AbstractApiResultBean {

    public static final String NOCYBER = "nocyber";
    public static final String CYBER = "cyber";

    public static final String QUERY_ID = "id";

    public static final String SET_WEIGHT = "weight";
    public static final String SET_ID = "id";
    public static final String SET_BANK_TYPE = "bank_type";

    private Integer id;
    private BankType bankId;                           //银行
    private String bankName;                           //银行名称
    private Integer bankClass;                       //1是网银，0是非网银
    private String weight;                             //排序权重

    public static BankWeight convertFromJSONObject(JSONObject object) {
        if (object == null) {
            return null;
        }
        BankWeight bankInfoSort = new BankWeight();
        bankInfoSort.id = getInt(object, "id");
        bankInfoSort.bankId = BankType.getItem(object.getInt("bank_id"));
        bankInfoSort.bankName = getString(object, "bank_name") ;
        bankInfoSort.bankClass = getInt(object, "bank_type") ;
        bankInfoSort.weight = getString(object, "weight") ;
        return bankInfoSort;
    }

    public static Map<String, List<BankWeight>> convertFromJSONArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        Map<String, List<BankWeight>> map = new HashMap<String, List<BankWeight>>();
        List<BankWeight> cyberList = new ArrayList<BankWeight>();
        List<BankWeight> nocyberList = new ArrayList<BankWeight>();
        for (Iterator<?> iterator = array.iterator(); iterator.hasNext();) {
            JSONObject object = (JSONObject) iterator.next();
            if (object.containsKey("nocyber")) {
                JSONArray nocyberJsonArray = (JSONArray)object.get("nocyber");
                if (nocyberJsonArray == null) {
                    return null;
                }
                for (Iterator<?> nocyberIterator = nocyberJsonArray.iterator(); nocyberIterator.hasNext();) {
                    JSONObject nocyberObject = (JSONObject) nocyberIterator.next();
                    nocyberList.add(convertFromJSONObject(nocyberObject));
                }

            }
            if (object.containsKey("cyber")) {
                JSONArray cyberJsonArray = (JSONArray)object.get("cyber");
                if (cyberJsonArray == null) {
                    return null;
                }
                for (Iterator<?> cyberIterator = cyberJsonArray.iterator(); cyberIterator.hasNext();) {
                    JSONObject cyberObject = (JSONObject) cyberIterator.next();
                    cyberList.add(convertFromJSONObject(cyberObject));
                }
            }
        }
        map.put(NOCYBER, nocyberList);
        map.put(CYBER, cyberList);
        return map;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public BankType getBankId() {
        return bankId;
    }

    public void setBankId(BankType bankId) {
        this.bankId = bankId;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public Integer getBankClass() {
        return bankClass;
    }

    public void setBankClass(Integer bankClass) {
        this.bankClass = bankClass;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }
}
